package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.player.DestroyRoleInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.AppIdChaneelInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.login.LoginProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.DestroyRoleMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_DESTROY_ROLE, desc = "注销角色")
public class DestroyRoleCmd extends AbstractCommand {
    private DestroyRoleInfo destroyRoleInfo = null;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        int systemOpenflag = ChannelConfig.DESTROY_ROLE_SWITCH;
        if (!StringUtils.isNullOrEmpty(player.getAppId())) {
            AppIdChaneelInfo appIdChaneelInfo = ServerListMgr.getAppIdChaneelInfo(player.getAppId());
            if (appIdChaneelInfo != null) {
                systemOpenflag = appIdChaneelInfo.isDestroyRoleLimit() ? 1 : 0;
            }
        }
        if (systemOpenflag != 1) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        UserInfo userInfo = player.getUserInfo();
        if(!StringUtils.isNullOrEmpty(userInfo.getUnionUid())){
            ret = GameErrorCode.E_HAS_UNION_CANNOT_DESTROY_ROLE;
            return;
        }
        destroyRoleInfo = DestroyRoleMgr.addDestroyRole(player.getUserId());
    }

    @Override
    public void sendData(GamePlayer player) {
        LoginProto.DestroyRoleRespMsg.Builder respMsg = LoginProto.DestroyRoleRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret && null != destroyRoleInfo){
            respMsg.setDestroyTime(destroyRoleInfo.getDestroyTime());
        }
        player.sendPacket(Protocol.U_DESTROY_ROLE, respMsg);
    }
}
